package com.scrx.ymg.service.direct;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.direct.IDirectDao;
import com.scrx.ymg.entity.Banner;
import com.scrx.ymg.entity.DirectInfo;
import com.scrx.ymg.entity.DirectMoney;
import com.scrx.ymg.entity.DirectProduct;
import com.scrx.ymg.entity.OtherInfo;
import com.scrx.ymg.entity.OtherMoney;
import com.scrx.ymg.util.BaseUtil;

@Service("directService")
public class DirectServiceImpl implements IDirectService {

	@Autowired
	private IDirectDao directDao;

	public String insertDirectInfo(DirectInfo info, MultipartFile licenseFile, MultipartFile identityCardFile,
			MultipartFile brandFile, MultipartFile qsImgFile, MultipartFile accreditFile) {
		DirectInfo oldinfo = directDao.directExists(info);

		if (oldinfo == null) {
			String directsPath = BaseUtil.ProductPath;

			String LicenseImg = BaseUtil.addRandom(licenseFile.getOriginalFilename());
			info.setLicenseImg(LicenseImg);
			String LicenseImgPath = directsPath + LicenseImg;

			String IdentityCardImg = BaseUtil.addRandom(identityCardFile.getOriginalFilename());
			info.setIdentityCardImg(IdentityCardImg);
			String IdentityCardImgPath = directsPath + IdentityCardImg;

			String BrandImg = BaseUtil.addRandom(brandFile.getOriginalFilename());
			info.setBrandImg(BrandImg);
			String BrandImgPath = directsPath + BrandImg;

			String QsImg = BaseUtil.addRandom(qsImgFile.getOriginalFilename());
			info.setQsImg(QsImg);
			String QsImgPath = directsPath + QsImg;

			String AccreditImg = BaseUtil.addRandom(accreditFile.getOriginalFilename());
			info.setAccreditImg(AccreditImg);
			String AccreditImgPath = directsPath + AccreditImg;

			DirectMoney money = new DirectMoney();
			money.setAccountMoney(0.0);
			money.setTotalMoney(0.0);

			String msg = directDao.insertDirectInfo(info, licenseFile, LicenseImgPath, identityCardFile,
					IdentityCardImgPath, brandFile, BrandImgPath, qsImgFile, QsImgPath, accreditFile, AccreditImgPath,
					money);
			return msg;
		} else {
			return "251";
		}
	}

	public String updateDirectInfoAll(DirectInfo info, MultipartFile licenseFile, MultipartFile identityCardFile,
			MultipartFile brandFile, MultipartFile qsImgFile, MultipartFile accreditFile) {
		DirectInfo oldinfo = directDao.getDirectInfoById(info.getId());
		if (oldinfo != null) {
			if (!info.getDirectName().equals(oldinfo.getDirectName())) {
				DirectInfo isnull = directDao.directExistsforName(info.getDirectName());
				if (isnull != null) {
					return "251";
				}
			}
			info.setAccount(oldinfo.getAccount());
			info.setPassWord(oldinfo.getPassWord());
			info.setStatus(oldinfo.getStatus());
			info.setRemark(oldinfo.getRemark());
			String directsPath = BaseUtil.ProductPath;
			String LicenseImgPath = "";
			if (licenseFile.getSize() > 0) {
				String LicenseImg = BaseUtil.addRandom(licenseFile.getOriginalFilename());
				info.setLicenseImg(LicenseImg);
				LicenseImgPath = directsPath + LicenseImg;
			} else {
				info.setLicenseImg(oldinfo.getLicenseImg());
			}
			String IdentityCardImgPath = "";
			if (identityCardFile.getSize() > 0) {
				String IdentityCardImg = BaseUtil.addRandom(identityCardFile.getOriginalFilename());
				info.setIdentityCardImg(IdentityCardImg);
				IdentityCardImgPath = directsPath + IdentityCardImg;
			} else {
				info.setIdentityCardImg(oldinfo.getIdentityCardImg());
			}
			String BrandImgPath = "";
			if (brandFile.getSize() > 0) {
				String BrandImg = BaseUtil.addRandom(brandFile.getOriginalFilename());
				info.setBrandImg(BrandImg);
				BrandImgPath = directsPath + BrandImg;
			} else {
				info.setBrandImg(oldinfo.getBrandImg());
			}

			String QsImgPath = "";
			if (qsImgFile.getSize() > 0) {
				String QsImg = BaseUtil.addRandom(qsImgFile.getOriginalFilename());
				info.setQsImg(QsImg);
				QsImgPath = directsPath + QsImg;
			} else {
				info.setQsImg(oldinfo.getQsImg());
			}
			String AccreditImgPath = "";
			if (accreditFile.getSize() > 0) {
				String AccreditImg = BaseUtil.addRandom(accreditFile.getOriginalFilename());
				info.setAccreditImg(AccreditImg);
				AccreditImgPath = directsPath + AccreditImg;
			} else {
				info.setAccreditImg(oldinfo.getAccreditImg());
			}
			String msg = directDao.updateDirectInfo(info, licenseFile, LicenseImgPath, identityCardFile,
					IdentityCardImgPath, brandFile, BrandImgPath, qsImgFile, QsImgPath, accreditFile, AccreditImgPath);
			return msg;
		} else {
			return "false";
		}
	}

	public String updateDirectInfo(DirectInfo info) {
		DirectInfo oldinfo = directDao.getDirectInfoById(info.getId());
		oldinfo.setStatus(info.getStatus());
		oldinfo.setRemark(info.getRemark());
		return directDao.updateDirectInfo(oldinfo);
	}

	public DirectMoney getDirectMoney(Integer id) {
		return directDao.getDirectMoney(id);
	}

	public String updateDirectInfoPassword(DirectInfo info, String newPassWord) {

		// DirectInfo oldinfo = directDao.directExistsByAccount(info);
		DirectInfo oldinfo = directDao.getDirectInfoById(info.getId());
		if (oldinfo != null && oldinfo.getPassWord().equals(info.getPassWord())) {
			oldinfo.setPassWord(newPassWord);
			return directDao.updateDirectInfo(oldinfo);
		} else {
			return "false";
		}

	}

	public String insertDirectProduct(DirectProduct product, MultipartFile adFile, MultipartFile thumbnailFile,
			MultipartFile detailsFile, MultipartFile introduce1File, MultipartFile introduce2File,
			MultipartFile introduce3File,List<MultipartFile> details) {
		DirectProduct temp = new DirectProduct();
		temp.setProductName(product.getProductName());
		Map<Object, Object> map = directDao.getDirectProduct(temp, 1, 10000);
		List<DirectProduct> list = (List<DirectProduct>) map.get("list");
		if (list != null && list.size() > 0) {// 商品名称已存在
			return "251";
		} else {
			String directsPath = BaseUtil.ProductPath;

			String adimg = BaseUtil.addRandom(adFile.getOriginalFilename());
			product.setAdImg(adimg);
			String AdImgPath = directsPath + adimg;

			String ThumbnailImg = BaseUtil.addRandom(thumbnailFile.getOriginalFilename());
			product.setThumbnailImg(ThumbnailImg);
			String ThumbnailImgPath = directsPath + ThumbnailImg;

			String DetailsImg = BaseUtil.addRandom(detailsFile.getOriginalFilename());
			product.setDetailsImg(DetailsImg);
			String DetailsImgPath = directsPath + DetailsImg;

			String introduce1 = BaseUtil.addRandom(introduce1File.getOriginalFilename());
			String introduce2 = BaseUtil.addRandom(introduce2File.getOriginalFilename());
			String introduce3 = BaseUtil.addRandom(introduce3File.getOriginalFilename());

			Banner banner = new Banner(introduce1, introduce2, introduce3);
			String introduce1Path = directsPath + introduce1;
			String introduce2Path = directsPath + introduce2;
			String introduce3Path = directsPath + introduce3;
			product.setEvaluateLevel(5);
			product.setStatus(1);
			product.setPutaway(0);
			product.setSubmitTime(BaseUtil.newdate());
			
			
			List<String> introduces = new ArrayList<String>();
			for (int i = 0; i < details.size(); i++) {
				introduces.add(BaseUtil.addRandom(details.get(i).getOriginalFilename()));
			}
			int size = details.size();
			switch (size) {
			case 9:
				product.setDetailsImg10(introduces.get(9 - 1));
			case 8:
				product.setDetailsImg9(introduces.get(8 - 1));
			case 7:
				product.setDetailsImg8(introduces.get(7 - 1));
			case 6:
				product.setDetailsImg7(introduces.get(6 - 1));
			case 5:
				product.setDetailsImg6(introduces.get(5 - 1));
			case 4:
				product.setDetailsImg5(introduces.get(4 - 1));
			case 3:
				product.setDetailsImg4(introduces.get(3 - 1));
			case 2:
				product.setDetailsImg3(introduces.get(2 - 1));
			case 1:
				product.setDetailsImg2(introduces.get(1 - 1));
			default:
				break;
			}
			String msg = directDao.insertDirectInfo(product, adFile, AdImgPath, thumbnailFile, ThumbnailImgPath,
					detailsFile, DetailsImgPath, introduce1File, introduce1Path, introduce2File, introduce2Path,
					introduce3File, introduce3Path, banner,details,introduces);
			return msg;
		}
	}

	public Map<Object, Object> getDirectProduct(DirectProduct product, int pageNum, int pageCount) {

		return directDao.getDirectProduct(product, pageNum, pageCount);
	}

	public String updateDirectProduct(DirectProduct product, MultipartFile adFile, MultipartFile thumbnailFile,
			MultipartFile detailsFile, MultipartFile introduce1File, MultipartFile introduce2File,
			MultipartFile introduce3File,List<MultipartFile> banners) {
		DirectProduct temp = new DirectProduct();
		temp.setId(product.getId());
		Map<Object, Object> map1 = directDao.getDirectProduct(temp, 1, 10000);
		List<DirectProduct> list1 = (List<DirectProduct>) map1.get("list");
		DirectProduct oldProduct = list1.get(0);
		if (product.getProductName().equals(list1.get(0).getProductName())) {// 如果没有修改商品名称
		} else {
			DirectProduct nameProduct = directDao.getDirectProductByName(product.getProductName());
			if (nameProduct != null) {// 商品名称已存在
				return "251";
			}
		}
		String AdImgPath = "";
		String ThumbnailImgPath = "";
		String DetailsImgPath = "";
		String directsPath = BaseUtil.ProductPath;
		if (adFile.getSize() > 0) {
			String AdImg = BaseUtil.addRandom(adFile.getOriginalFilename());
			product.setAdImg(AdImg);
			AdImgPath = directsPath + AdImg;
		} else {
			product.setAdImg(oldProduct.getAdImg());
			AdImgPath = directsPath + oldProduct.getAdImg();
		}

		if (thumbnailFile.getSize() > 0) {
			String ThumbnailImg = BaseUtil.addRandom(thumbnailFile.getOriginalFilename());
			product.setThumbnailImg(ThumbnailImg);
			ThumbnailImgPath = directsPath + ThumbnailImg;
		} else {
			product.setThumbnailImg(oldProduct.getThumbnailImg());
			ThumbnailImgPath = directsPath + oldProduct.getThumbnailImg();
		}

		if (detailsFile.getSize() > 0) {
			String DetailsImg = BaseUtil.addRandom(detailsFile.getOriginalFilename());
			product.setDetailsImg(DetailsImg);
			DetailsImgPath = directsPath + DetailsImg;
		} else {
			product.setDetailsImg(oldProduct.getDetailsImg());
			DetailsImgPath = directsPath + oldProduct.getDetailsImg();
		}

		String introduce1Path = "";
		String introduce2Path = "";
		String introduce3Path = "";
		Banner banner = directDao.getBannerByProductId(product.getId());
		if (introduce1File.getSize() > 0) {
			String Picture1 = BaseUtil.addRandom(introduce1File.getOriginalFilename());
			banner.setPicture1(Picture1);
			introduce1Path = directsPath + Picture1;
		}
		if (introduce2File.getSize() > 0) {
			String Picture2 = BaseUtil.addRandom(introduce2File.getOriginalFilename());
			banner.setPicture2(Picture2);
			introduce2Path = directsPath + Picture2;
		}
		if (introduce3File.getSize() > 0) {
			String Picture3 = BaseUtil.addRandom(introduce3File.getOriginalFilename());
			banner.setPicture3(Picture3);
			introduce3Path = directsPath + Picture3;
		}
		product.setSales(oldProduct.getSales());//销量
		product.setEvaluateLevel(oldProduct.getEvaluateLevel());//评价
		product.setStatus(1);
		product.setPutaway(0);
		
		for (int i = 0; i < banners.size(); i++) {
			if (banners.get(i) != null) {
				if (i == 0) {
					product.setDetailsImg2(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 1) {
					product.setDetailsImg3(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 2) {
					product.setDetailsImg4(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 3) {
					product.setDetailsImg5(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 4) {
					product.setDetailsImg6(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 5) {
					product.setDetailsImg7(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 6) {
					product.setDetailsImg8(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				} else if (i == 7) {
					product.setDetailsImg9(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				}else if (i == 8) {
					product.setDetailsImg10(BaseUtil.addRandom(banners.get(i).getOriginalFilename()));
				}
			}else{
				if (i == 0) {
					product.setDetailsImg2(oldProduct.getDetailsImg2());
				} else if (i == 1) {
					product.setDetailsImg3(oldProduct.getDetailsImg3());
				} else if (i == 2) {
					product.setDetailsImg4(oldProduct.getDetailsImg4());
				} else if (i == 3) {
					product.setDetailsImg5(oldProduct.getDetailsImg5());
				} else if (i == 4) {
					product.setDetailsImg6(oldProduct.getDetailsImg6());
				} else if (i == 5) {
					product.setDetailsImg7(oldProduct.getDetailsImg7());
				} else if (i == 6) {
					product.setDetailsImg8(oldProduct.getDetailsImg8());
				} else if (i == 7) {
					product.setDetailsImg9(oldProduct.getDetailsImg9());
				}else if (i == 8) {
					product.setDetailsImg10(oldProduct.getDetailsImg10());
				}
				
			}

		}
		List<String> introduces = new ArrayList<String>();
		for (int i = 0; i < banners.size(); i++) {
			if(banners.get(i)==null){
				introduces.add(null);
			}else{
				if(i==0){
					introduces.add(product.getDetailsImg2());
				}else if(i==1){
					introduces.add(product.getDetailsImg3());
				}else if(i==2){
					introduces.add(product.getDetailsImg4());
				}else if(i==3){
					introduces.add(product.getDetailsImg5());
				}else if(i==4){
					introduces.add(product.getDetailsImg6());
				}else if(i==5){
					introduces.add(product.getDetailsImg7());
				}else if(i==6){
					introduces.add(product.getDetailsImg8());
				}else if(i==7){
					introduces.add(product.getDetailsImg9());
				}else if(i==8){
					introduces.add(product.getDetailsImg10());
				}
			}
		}
		
		
		
		
		String msg = directDao.updateDirectInfo(product, adFile, AdImgPath, thumbnailFile, ThumbnailImgPath,
				detailsFile, DetailsImgPath, introduce1File, introduce1Path, introduce2File, introduce2Path,
				introduce3File, introduce3Path, banner,banners,introduces);
		return msg;

	}

	public Map<Object, Object> getDirectInfo(DirectInfo info, int pageNum, int pageCount) {
		return directDao.getDirectInfo(info, pageNum, pageCount);
	}

	public String checkDirectProduct(DirectProduct product) {
		DirectProduct oldproduct = directDao.getDirectProductById(product.getId());
		if (product.getStatus() != null && product.getStatus() == 1) {
			oldproduct.setPutaway(1);
			oldproduct.setStatus(product.getStatus());
		}
		if (product.getPutaway() != null) {
			oldproduct.setPutaway(product.getPutaway());
		}
		return directDao.updateObject(oldproduct);
	}

	public DirectInfo getDirectInfoById(Integer id) {
		return directDao.getDirectInfoById(id);
	}

	public DirectProduct getDirectProductById(int productId) {
		return directDao.getDirectProductById(productId);
	}

	public Banner getBannerByProductId(Integer id) {
		return directDao.getBannerByProductId(id);
	}

}
